//
//  TongJIDetailVC.m
//  ZhiBoYuan
//
//  Created by coder_di on 2018/5/7.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "TongJIDetailVC.h"
#import "TanChuCollectionReusableView.h"
#import "TanChuCollectionViewCell.h"

@interface TongJIDetailVC ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong)UICollectionView *collectionView;

@property (nonatomic, strong)UICollectionViewFlowLayout *flowLayout;

@property (nonatomic, strong)NSMutableArray *tiArray;

@property (nonatomic, strong)NSDictionary *dataDictionary;

@end

@implementation TongJIDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(backAction1)];
    
    [self.view addGestureRecognizer:tap];
    
    [self setcollectionView];
    
    [self loadData];
    
}

-(void)loadData{

    _tiArray = [NSMutableArray array];
    _dataDictionary = [NSDictionary dictionary];
    
    NSString *url = [NSString string];
    
    SGParameter *parameter  = [[SGParameter alloc] init];
    
    if (_iscomeFromTi == YES) {
        
        url = @"g=Client&m=Task&a=studentPaperReport";
        
        parameter.body = @{@"task_id":_task_id,
                           @"paper_id":_course_id,
                           @"student_id":_student_id
                           };
    }else{
        url = @"g=Client&m=Task&a=studentCourseReport";
        parameter.body = @{@"task_id":_task_id,
                           @"course_id":_course_id,
                           @"student_id":_student_id
                           };
        
    }
    
   
    [SGHttpTool POST:[NSString stringWithFormat:@"%@%@",HTTPP,url] parameter:parameter sucess:^(id responseObject) {
        
        NSLog(@"%@",responseObject);
        
        _dataDictionary = responseObject[@"data"];
        NSDictionary *dic = responseObject;
        
        if ([dic[@"status"] intValue] == 200) {
            
            for (NSDictionary *dicc in dic[@"data"][@"question_list"]) {
                [_tiArray addObject:dicc];
            }
            
            [self.collectionView reloadData];
        }else{
            [MBProgressHUD showTextOnly:self.view message:dic[@"msg"] delay:2.0f];
        }
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
    
}


-(void)setcollectionView{
    //设置布局对象
    _flowLayout = [[UICollectionViewFlowLayout alloc]init];
    //设置滚动方向
    _flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    
    //设置item大小
    _flowLayout.itemSize = CGSizeMake(30 * KscreenWidth, 30 * KscreenHeight);
    
    //设置最小列间距
    _flowLayout.minimumInteritemSpacing = 40 * KscreenWidth;
    
    //设置最小行间距
    _flowLayout.minimumLineSpacing = 30 * KscreenHeight;
    
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(30 * KscreenWidth,150 * KscreenHeight, ScreenWidth - 60 * KscreenWidth,  400 * KscreenHeight) collectionViewLayout:_flowLayout];
    
    _collectionView.backgroundColor = [UIColor whiteColor];
    
    _collectionView.layer.masksToBounds = YES;
    _collectionView.layer.cornerRadius = 5;
    
    //设置代理
    _collectionView.delegate = self;
    
    //设置数据源
    _collectionView.dataSource = self;
    
    //注册
    [_collectionView registerClass:[TanChuCollectionViewCell class] forCellWithReuseIdentifier:@"TanChuCollectionViewCell"];
    
#pragma mark -- 注册头部视图
    [_collectionView registerClass:[TanChuCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"TanChuCollectionReusableView"];
    
    //添加到视图
    [self.view addSubview:_collectionView];
    
//    UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
//    btn.frame = CGRectMake(screenWidth - 70 * KscreenWidth, 0, 10 * KscreenWidth, 10 * KscreenHeight);
//    [btn setBackgroundImage:[UIImage imageNamed:@"关闭-2"] forState:0];
//    [btn addTarget:self action:@selector(closeAction) forControlEvents:(UIControlEventTouchUpInside)];
//    [_collectionView addSubview:btn];
}

-(void)closeAction{
    [self dismissViewControllerAnimated:NO completion:^{
        
    }];
    
}

#pragma mark 必须实现的方法
//每分区有多少个item
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

        return _tiArray.count;
    
}

//每个item上面现实的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
        //从重用池里面获取cell
        TanChuCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TanChuCollectionViewCell" forIndexPath:indexPath];
    
    cell.diccc = _tiArray[indexPath.row];
    
    cell.backView.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row + 1];
        
        return cell;
    
}

//设置分区数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

//点击item触发的事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 2) {
        TongJIDetailVC *tongjiDetailVC = [[TongJIDetailVC alloc]init];
        tongjiDetailVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        [self presentViewController:tongjiDetailVC animated:NO completion:^{
            tongjiDetailVC.view.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
            tongjiDetailVC.view.alpha = 0.5;
        }];
    }
}

//返回分区内边距的上左下右的距离
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{

        return UIEdgeInsetsMake(0, 30 *KscreenHeight, 30 * KscreenWidth, 30 * KscreenHeight);
    
}


-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{

        CGSize size = CGSizeMake(SCREEN_WIDTH, 200 * KscreenHeight);
        return size;
    
    
}

#pragma mark - 头部视图内容
//设置头尾部内容

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

    UICollectionReusableView *reusableView =nil;

    if (kind ==UICollectionElementKindSectionHeader) {

        TanChuCollectionReusableView *headV = (TanChuCollectionReusableView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"TanChuCollectionReusableView" forIndexPath:indexPath];

        headV.dataDic = _dataDictionary;

        reusableView = headV;

    }

    return reusableView;

}




-(void)backAction1{
    [self dismissViewControllerAnimated:NO completion:^{
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
